var id=document.URL.split("=")[1];

var sid=null;
var staffId=null;
var name=null;
var password=null;
var sex=null;
var age=null;
var positionid=null;
var deptId=null;
var tel=null;
var email=null;
var hobby=null;
var selfPartiality=null;
var roleId=null;
var staffStatus=null;
var flag=null;

var vm = new Vue({
    el:"#updateName",
    data:{
        message:'',
        depts:'',
        positions:'',
    },
    // 方法存放区
    methods: {

        getAllNameCardListInfo() {
            axios({
                method: 'POST',
                url: 'http://localhost:8099/personalNameCard/query',
                data:{
                    "id":id
                }
            }).then(function (result) {
                vm.message = result.data;
                var a=vm.message;
                sid=a.id;
                staffId=a.staffId;
                // name=a.name;
                password=a.password;
                sex=a.sex;
                age=a.age;
                // positionid=a.positionid;
                // deptId=a.deptId;
                // tel=a.tel;
                // email=a.email;
                hobby=a.hobby;
                selfPartiality=a.selfPartiality;
                roleId=a.roleId;
                staffStatus=a.staffStatus;
                flag=a.flag;
                // console.log(vm.nameCardList)
            }).catch(function (reason) {
                console.log(reason);
            })

            //获取所有部门
            axios({
                method: 'POST',
                url: 'http://localhost:8099/personalNameCard/getAllDept',
            }).then(function (result) {
                vm.depts = result.data;
                // console.log(vm.nameCardList)
            }).catch(function (reason) {
                console.log(reason);
            })

            //获取所有职位
            axios({
                method: 'POST',
                url: 'http://localhost:8099/personalNameCard/getPosition',
            }).then(function (result) {
                vm.positions = result.data;
                // console.log(vm.nameCardList)
            }).catch(function (reason) {
                console.log(reason);
            })

        },



    },

    //页面加载完自动执行
    mounted(){
        this.getAllNameCardListInfo();
    }

})




function submit() {
    name = $(".forminfo").children("li:nth-child(1)").children("input").val();
    positionid = $(".forminfo>li:nth-child(2)>select>option:checked").attr("power");
    deptId = $(".forminfo>li:nth-child(3)>select>option:checked").attr("power");
    tel = $(".forminfo").children("li:nth-child(4)").children("input").val();
    email = $(".forminfo").children("li:nth-child(5)").children("input").val();
    console.log(name)
    console.log(positionid)
    console.log(deptId)
    console.log(tel)
    console.log(email)


    console.log(id)
    console.log(staffStatus)
    console.log(staffId)
    console.log(password)
    console.log(sex)
    console.log(age)
    console.log(hobby)
    console.log(selfPartiality)
    console.log(roleId)
    console.log(flag)

    var telzhenzeflag =/^1[3-9]{1}[0-9]{9}$/;
    var emailzhenzeflag =/^([a-zA-Z0-9_\-\.])+\@([a-zA-Z0-9_\-\.])+\.([a-zA-Z]{2,8})$/;


    checkTel(tel).then(n=>{
        checkEmail(email).then(n1=>{
            if(telzhenzeflag.test(tel)&&emailzhenzeflag.test(email)){
                if(n&&n1){
                    axios.post(
                        "http://localhost:8099/personalNameCard/update",
                        {
                            id:id,
                            staffId:staffId,
                            name:name,
                            password:password,
                            sex:sex,
                            age:age,
                            positionid:positionid,
                            deptId:deptId,
                            tel:tel,
                            email:email,
                            hobby:hobby,
                            selfPartiality:selfPartiality,
                            roleId:roleId,
                            staffStatus:staffStatus,
                            flag:flag
                        }
                    ).then(
                        function(result){
                            if(result.data==true){
                                alert("员工信息更改成功！");
                                location.href="personalNameCardList.html"
                            }else if(result.data==false){
                                alert("员工信息更改失败！");
                            }
                        },function (err) {
                            console.log(err);
                        }
                    )
                }
            }else{
                alert("联系方式填写格式错误！请确认！");
            }
        })
    })
}



function checkEmail(n) {
    return axios.post(
        "http://localhost:8099/PersonalInfo/checkEmail",
        {email:n}
    ).then(
        function(result){
            console.log(result.data);
            if(result.data!==""){
                alert("邮箱已存在！")
                return false;
            }else {
                return true;
            }
        },function (err) {
            console.log(err);
        }
    )
}

function checkTel(n) {
    return axios.post(
        "http://localhost:8099/PersonalInfo/checkTel",
        {tel:n}
    ).then(
        function(result){
            if(result.data!=""){
                alert("电话号码已存在！");
                return false;
            }else {
                return true;
            }
        },function (err) {
            console.log(err);
        }
    )
}